// pages/pointRecord/pointRecord.js
const { default: request } = require("../../utils/request");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    currentPage: 1, // 当前页码
    pageSize: 10, // 每页数据条数
  },
  onReachBottom:function() {
      // 触发加载更多数据的逻辑
      this.setData({
        currentPage: this.data.currentPage + 1,
      });
      this.loadMoreData()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  goback() {
    wx.navigateBack({
      delta: 1,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.loadMoreData();
    this.setData({
      scrollHeight: 100
    });
  },
  async loadData(page) {
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-user-api/member/point/record/page?pageSize=${10}&pageNo=${
          this.data.currentPage
        }`,
      method: "GET",
    });
    const list = res.data.list
    const item = list.map((item) => {
      let data = new Date(item.createTime)
      let year = data.getFullYear()
      let month = (data.getMonth() + 1).toString().padStart(2, "0")
      let day = data.getDate().toString().padStart(2, "0")
      let formattedDateTime = `${year}-${month}-${day}`
      item.createTime = formattedDateTime
      return item
    })
    this.setData({
      dataList: item,
    })
  },
  async loadMoreData() {
    const that = this;
    this.setData({ loading: true });
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-user-api/member/point/record/page?pageSize=${100}&pageNo=${
          this.data.currentPage
        }`,
      method: "GET",
    });
    if (res.data.length > 0) {
      const newDataList = that.data.dataList.concat(data);
      that.setData({
        dataList: newDataList,
        currentPage: that.data.currentPage + 1,
        loading: false,
      });
    } else {
      that.setData({ hasMoreData: false });
    }
    const list = res.data.list;
    const item = list.map((item) => {
      let data = new Date(item.createTime);
      let year = data.getFullYear();
      let month = (data.getMonth() + 1).toString().padStart(2, "0");
      let day = data.getDate().toString().padStart(2, "0");
      let formattedDateTime = `${year}-${month}-${day}`;
      item.createTime = formattedDateTime;
      return item;
    });
    this.setData({
      dataList: this.data.dataList.concat(item),
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
